package com.ympc.service.impl;

import com.ympc.mapper.TeacherMapper;
import com.ympc.mapper.UsersMapper;
import com.ympc.pojo.Teacher;
import com.ympc.pojo.Users;
import com.ympc.service.TeacherService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    
    @Resource
    private TeacherMapper teacherMapper;
    
    @Resource
    private UsersMapper usersMapper;
    
    @Override
    public Teacher getTeacherByUserId(Integer userId) {
        return teacherMapper.findByUserId(userId);
    }
    
    @Override
    public Teacher getTeacherById(Integer id) {
        return teacherMapper.findById(id);
    }
    
    @Override
    public boolean updateTeacherInfo(Teacher teacher) {
        return teacherMapper.updateTeacher(teacher) > 0;
    }
    
    @Override
    public boolean changePassword(Integer userId, String oldPassword, String newPassword) {
        // 验证旧密码
        Users dbUser = usersMapper.findById(userId);
        if (dbUser == null || !dbUser.getPassword().equals(oldPassword)) {
            return false;
        }
        // 更新密码
        return teacherMapper.updatePassword(userId, newPassword) > 0;
    }
    
    @Override
    public boolean createTeacherInfo(Teacher teacher) {
        return teacherMapper.insertTeacher(teacher) > 0;
    }
    
    @Override
    public List<Teacher> getAllTeachers() {
        return teacherMapper.findAll();
    }
}

